/**
 * Copyright 2011 FeedDreamwork SIG
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.feeddreamwork.processors;

public final class FeedProcessorBuilder {
	private static final String PARAMETER_SPLITER = "##";
	private static final String NO_DELETE_SIGN = "%%NO_DELETE%%";

	public static FeedProcessor getProcessor(String command) {
		if (command.startsWith("addafter")) {
			String [] params = parseCommand(command, "addafter", 1);
			return new AddAfterProcessor(params[0], params[1]);
		}
		if (command.startsWith("addbefore")) {
			String [] params = parseCommand(command, "addbefore", 1);
			return new AddBeforeProcessor(params[0], params[1]);
		}
		if (command.startsWith("addentrydataafter")) {
			String[] params = parseCommand(command, "addentrydataafter", 2);
			return new AddEntrydataAfterProcessor(params[0], params[1], params[2]);
		}
		if (command.startsWith("addentrydatabefore")) {
			String[] params = parseCommand(command, "addentrydatabefore", 2);
			return new AddEntrydataBeforeProcessor(params[0], params[1], params[2]);
		}
		if (command.startsWith("createfeed")) {
			String[] parameters = command.replaceFirst("createfeed ", "").split(
					PARAMETER_SPLITER);
			return new CreateFeedProcessor(parameters[0], parameters[1]);
		}
		if (command.startsWith("feedfilter")) {
			String[] parameters = command.replaceFirst("feedfilter ", "").split(" ");
			return new FeedFilterProcessor(parameters[0],
					command.replaceFirst("feedfilter " + parameters[0] + " ", ""));
		}
		if (command.startsWith("fetchfeed"))
			return new FetchFeedProcessor(command.replaceFirst("fetchfeed ", ""));
		if (command.startsWith("fetchpage"))
			return new FetchPageProcessor();
		if (command.startsWith("getlatest"))
			return new GetLatestProcessor(Integer.parseInt(command.replaceFirst(
					"getlatest ", "")));
		if (command.startsWith("mergehtml")) {
			String [] params = command.replaceFirst("mergehtml ", "").split(" ");
			return new MergeHtmlProcessor(params[0], command.replace("mergehtml " + params[0] + " ", ""));
		}
		if (command.startsWith("mergefeed"))
			return new MergeFeedProcessor(command.replaceFirst("mergefeed ", ""));
		if (command.startsWith("pubsubhubbub"))
			return new PubsubhubbubProcessor();
		if (command.startsWith("removeredundancy"))
			return new RemoveRedundancyProcessor();
		if (command.startsWith("remove")) {
			String [] params = parseCommand(command, NO_DELETE_SIGN, 2);
			boolean incself = false;
			if (params[2].startsWith("incself ")) {
				params[2] = params[2].replaceFirst("incself ", "");
				incself = true;
			}
			if (params[0].equals("removeafter"))
				return new RemoveAfterProcessor(params[1], params[2], incself);
			if (params[0].equals("removeafterregex"))
				return new RemoveAfterRegexProcessor(params[1], params[2], incself);
			if (params[0].equals("removebefore"))
				return new RemoveBeforeProcessor(params[1], params[2], incself);
			if (params[0].equals("removebeforeregex"))
				return new RemoveBeforeRegexProcessor(params[1], params[2], incself);
		}
		if (command.startsWith("replace")) {
			String [] params = parseCommand(command, "replace", 1);
			boolean first = false;
			if (params[1].startsWith("first ")) {
				first = true;
				params[1] = params[1].replaceFirst("first ", "");
			}
			String [] replaces = params[1].split(PARAMETER_SPLITER);
			if (replaces.length == 1)
				return new ReplaceProcessor(params[0], replaces[0], "", first);
			return new ReplaceProcessor(params[0], replaces[0], replaces[1], first);
		}
		if (command.startsWith("setentrydata")) {
			String[] params = parseCommand(command, "setentrydata", 2);
			return new SetEntrydataProcessor(params[0], params[1], params[2]);
		}
		if (command.startsWith("setmetadata")) {
			String[] params = parseCommand(command, "setmetadata", 1);
			return new SetMetadataProcessor(params[0], params[1]);
		}
		if (command.startsWith("sortentry"))
			return new SortEntryProcessor();
		if (command.startsWith("timezone"))
			return new TimezoneProcessor(command.replaceFirst("timezone ", ""));
		return null;
	}
	
	private static String [] parseCommand(String command, String funct, int argn) {
		if (!funct.equals(NO_DELETE_SIGN))
			command = command.replaceFirst(funct + " ", "");
		String [] params = command.split(" ");
		String [] ans = new String [argn + 1];
		for (int i = 0; i < argn && i < params.length; i++) {
			ans[i] = params[i];
			command = command.replaceFirst(params[i] + " ", "");
		}
		ans[argn] = command;
		return ans;
	}

	private FeedProcessorBuilder() {
	}
}
